#include <stdio.h>
#include <stdlib.h>

/*
 * 深度优先搜索
 */

#define vertex_max 8
#define edge_max   10

#define true 1
#define false 0

unsigned char visited[vertex_max + 1];

struct graph
{
  int vertex[vertex_max + 1];
  int arcs[vertex_max + 1][vertex_max + 1];
};

void adjacency_matrix(struct graph * graph)
{
  int i, k, j;
  
  for (i = 1; i <= vertex_max; i++)
    scanf("%d", &graph->vertex[i]);

  for (i = 1; i <= vertex_max; i++)
    for (j = 1; j <= vertex_max; j++)
      graph->arcs[i][j] = 0;

  for (k = 1; k <= edge_max; k++) {
    scanf("%d %d", &i, &j);
    graph->arcs[i][j] = 1;
    graph->arcs[j][i] = 1;
  }
}

void deep_first_search(struct graph * graph, int i)
{
  int j;

  printf(" %d ", graph->vertex[i]);
  visited[i] = true;
  for (j = 1; j <= vertex_max; j++)
    if ((graph->arcs[i][j] == 1) && ( !visited[j]))
      deep_first_search(graph, j);
}

void display(struct graph * graph, int vexter)
{
  int i, j, count;

  count = 0;
  for (i = 1; i <= vertex_max; i++)
    for (j = 1; j <= vertex_max; j++) {
      printf(" %d ", graph->arcs[i][j]);
      count++;
      if (count % vexter == 0) 
	printf("\n");
    }

  printf("\n");
}


void main()
{
  struct graph  graph;
  int i;

  adjacency_matrix( &graph);
  display( &graph, 8);

  for (i = 1; i <= vertex_max; i++)
    visited[i] = false;

  deep_first_search( &graph, 1);
  printf("\n");
}
